package com.bocai.grainshop.member.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.bocai.common.exception.CodeMnume;
import com.bocai.grainshop.member.exception.EmailException;
import com.bocai.grainshop.member.exception.UsernameException;
import com.bocai.grainshop.member.vo.MemberUserLoginVo;
import com.bocai.grainshop.member.vo.MemberUserRegisterVo;
import com.bocai.grainshop.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bocai.grainshop.member.entity.MemberEntity;
import com.bocai.grainshop.member.service.MemberService;
import com.bocai.common.utils.PageUtils;
import com.bocai.common.utils.R;



/**
 * 会员
 *
 * @author SunJinBo
 * @email 2374461077@qq.com
 * @date 2022-03-01 20:18:42
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * gitee登录
     */
    @PostMapping("/gitee")
    public R GiteeLogin(@RequestBody SocialUser socialUser){
        MemberEntity memberEntity = memberService.GiteeLogin(socialUser);
        if(memberEntity!=null){
            return R.ok().put("user",memberEntity);
        }else {
            return R.error(CodeMnume.USER_LOGIN_ERROR.getCode(),CodeMnume.USER_LOGIN_ERROR.getMessage());
        }
    }

    /**
     * 会员登录
     */
    @PostMapping("/login")
    public R loginMember(@RequestBody MemberUserLoginVo loginVo){
        MemberEntity entity = memberService.login(loginVo);
        if(entity!=null){
            return R.ok().put("user",entity);
        }else {
            return R.error(CodeMnume.USER_LOGIN_ERROR.getCode(),CodeMnume.USER_LOGIN_ERROR.getMessage());
        }
    }

    /**
     * 会员注册
     */
    @PostMapping("/regist")
    public R registMember(@RequestBody MemberUserRegisterVo registerVo){
        try {
            memberService.registMember(registerVo);
        } catch (EmailException e) {
            return R.error(CodeMnume.EMAIL_ERROR.getCode(),CodeMnume.EMAIL_ERROR.getMessage());
        } catch (UsernameException e){
            return R.error(CodeMnume.USER_NMAE_ERROR.getCode(),CodeMnume.USER_NMAE_ERROR.getMessage());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
